﻿Public Class Frm_Rech_Plainte
    'weather

    Dim ref As Integer
    Private Sub txt_rech_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_rech.TextChanged
        List_tous.Items.Clear()
        'Comb_nom.SelectedIndex = -1
        'Comb_adv.SelectedIndex = -1
        'Partager.cltRech = True
        'Partager.advRech = True

        Dim varBol As Boolean = False
        If ref = -1 Then
            varBol = True
        End If

        For Each plnt As Plainte In Partager.moi.List_Plainte
            plnt.remplir_Plainte_Ref()
            For Each refPlt As Plainte_Ref In plnt.list_Plainte_Ref
                If varBol = True Or ref = CInt(refPlt.ref) Then
                    If plnt.Num_Plainte.ToString.Contains(txt_rech.Text) Then
                        List_tous.Items.Add(New ListViewItem(New String() {plnt.Id, plnt.Num_Plainte, plnt.Tribunal}))
                    End If
                End If
            Next
        Next

        If txt_rech.Text = "" Then
            List_tous.Items.Clear()
        End If
    End Sub

    Private Sub frmRechProce_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If Partager.forPlainteRech Then
            frmProces.Enabled = True
            Partager.forPlainteRech = False
        Else
            Frm_Plainte.Enabled = True
        End If
        ref = -1
    End Sub

    Private Sub List_tous_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles List_tous.DoubleClick, Btn_choisi.Click
        If List_tous.SelectedIndices.Count > 0 Then
            Dim idPlnt As Integer = CInt(List_tous.SelectedItems(0).SubItems(0).Text)
            Dim plntReq As IEnumerable(Of Plainte) = From plnt As Plainte In Partager.moi.List_Plainte
                                                   Where plnt.Id = idPlnt


            If Partager.forPlainteRech Then
                frmProces.cmbPlainte.Text = plntReq(0).Num_Plainte & "/" & plntReq(0).Type.Split("-")(0) & "/" & plntReq(0).Annee
            Else
                For Each ligne As ListViewItem In Frm_Plainte.List_tous.Items
                    If CInt(ligne.SubItems(0).Text) = plntReq(0).Id Then
                        ligne.Selected = True
                        Exit For
                    End If
                Next
            End If
            Me.Close()
        End If
    End Sub

    Private Sub Frm_Rech_Plainte_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Partager.remplirClient(Comb_nom)
        Partager.remplirEnemie(Comb_adv)
        '-------------------------------
        Comb_nom.SelectedIndex = -1
        Partager.deClt.Key = -1
        Comb_adv.SelectedIndex = -1
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechClt.Click
        Partager.remplirClient(Comb_nom)
        Partager.cltRech = True
        Partager.advRech = False
        Me.Enabled = False
        Partager.frmMoule = New Form
        Partager.frmMoule = Me
        Partager.type_rech_mowakil = "Frm_Ref"
        Frm_Rech_Clients.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Partager.remplirEnemie(Comb_adv)
        Partager.cltRech = False
        Partager.advRech = True
        Me.Enabled = False
        Partager.frmMoule = New Form
        Partager.frmMoule = Me
        Partager.type_rech_adver = "Frm_Idafa_khossoum"
        Frm_Rech_Adver.Show()
    End Sub

    Private Sub Comb_nom_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_nom.SelectedIndexChanged
        List_tous.Items.Clear()
        If Comb_nom.SelectedIndex >= 0 Then
            Partager.deClt = DirectCast(Comb_nom.SelectedItem, DictionaryEntry)
            If Partager.cltRech Then
                Partager.advRech = False
                Comb_adv.Items.Clear()
                Partager.remplir_CombAdvr(CInt(Partager.deClt.Key), Comb_adv)
                If Comb_adv.Items.Count <= 0 Then
                    ref = -1
                End If
            End If

            If Partager.advRech Then
                If Comb_adv.SelectedIndex >= 0 Then
                    Dim idAdv As Integer = DirectCast(Comb_adv.SelectedItem, DictionaryEntry).Key
                    ref = Partager.refParties(Partager.deClt.Key, idAdv)
                    remplirLstViRef()
                Else
                    ref = -1
                End If
            End If
        End If
    End Sub

    Public Sub remplirLstViRef()
        For Each plnt As Plainte In Partager.moi.List_Plainte
            plnt.remplir_Plainte_Ref()
            For Each refPlt As Plainte_Ref In plnt.list_Plainte_Ref
                If ref = CInt(refPlt.ref) Then
                    List_tous.Items.Add(New ListViewItem(New String() {plnt.Id, plnt.Num_Plainte, plnt.Tribunal}))
                End If
            Next
        Next
    End Sub

    Private Sub Comb_adv_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_adv.SelectedIndexChanged
        List_tous.Items.Clear()
        If Comb_adv.SelectedIndex >= 0 Then
            Partager.deClt = DirectCast(Comb_adv.SelectedItem, DictionaryEntry)
            If Partager.advRech Then
                Partager.cltRech = False
                Comb_nom.Items.Clear()
                Partager.remplir_CombClient(CInt(Partager.deClt.Key), Comb_nom)
                If Comb_nom.Items.Count <= 0 Then
                    ref = -1
                End If
            End If

            If Partager.cltRech Then
                If Comb_nom.SelectedIndex >= 0 Then
                    Dim idclt As Integer = DirectCast(Comb_nom.SelectedItem, DictionaryEntry).Key
                    ref = Partager.refParties(idclt, Partager.deClt.Key)
                    remplirLstViRef()
                Else
                    ref = -1
                End If
            End If
        Else
            ref = -1
        End If

    End Sub
    Private Sub btnNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNouv.Click
        txt_rech.Text = ""
        Comb_nom.SelectedIndex = -1
        Comb_adv.SelectedIndex = -1
        ref = -1
        Partager.cltRech = True
        Partager.advRech = True
    End Sub

End Class